package com.itheima.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.DishDto;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.SetmealDish;
import com.itheima.pojo.SetmealDto;
import com.itheima.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐相关接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 添加套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增套餐接口")
    public R<String> saveSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.saveSetmeal(setmealDto);
        return R.success("保存套餐成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "套餐分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public R<Page<SetmealDto>> pageSelect(Integer page, Integer pageSize, String name){
        Page<SetmealDto> setmealDtoPage = setmealService.pageSelect(page,pageSize,name);
        return R.success(setmealDtoPage);
    }

    /**
     * (批量)删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache",allEntries = true)
    @ApiOperation(value = "套餐删除接口")
    public R deleteDish(@RequestParam List<Long> ids){
        return setmealService.deleteByIds(ids);
    }

    /**
     * (批量)起售/停售套餐
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> stopSellingSetmeal(@PathVariable Integer status,@RequestParam List<Long> ids){
        setmealService.updateStatus(status,ids);
        return R.success("操作成功");
    }

    /**
     * 根据套餐id查询套餐数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> selectSetmealById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getSetmealDto(id);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> updateSetmeal(@RequestBody SetmealDto setmealDto){
        setmealService.updateSetmealDto(setmealDto);
        return R.success("修改成功");
    }

    /**
     * 根据套餐分类id查询该套餐分类下的套餐展示在客户端
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "套餐条件查询接口")
    public R<List<Setmeal>> selectSetmealByCategoryId(Long categoryId, Integer status){
        List<Setmeal> setmeals = setmealService.selectSetmeal(categoryId,status);
        return R.success(setmeals);
    }

    /**
     * 根据套餐id查询该套餐下的菜品
     * @param setmealId
     * @return
     */
    @GetMapping("/dish/{setmealId}")
    public R<List<SetmealDish>> selectSetmealDishByCategoryId(@PathVariable Long setmealId){
        List<SetmealDish> setmealDishes = setmealService.selectSetmealDish(setmealId);
        return R.success(setmealDishes);
    }
}
